﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SPUnlockSandbox.SPFileInfo
{
    [ToolboxItem(false)]
    public partial class SPFileInfo : System.Web.UI.WebControls.WebParts.WebPart
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            txtAreaLog.Text = "Start";
        }
        protected void btnLockInfo_Click(object sender, EventArgs e)
        {
            try
            {
                Logg("Fetching Log Info");
                SPWeb web = SPContext.Current.Web;
                SPFile File = web.GetFile(txtDocUrl.Text);
                if (String.IsNullOrEmpty(File.LockId))
                {
                    Logg(File.Title + " Is not Locked");
                    if (File.CheckOutType != SPFile.SPCheckOutType.None)
                    {
                        Logg("But "+ File.Title + " is Checked Out to user " + File.CheckedOutByUser.LoginName);
                        Logg(" Checked Out Type: " + File.CheckOutType);
                        Logg(" Checked Out CheckedOutDate: " + File.CheckedOutDate);
                        Logg(" File can be Checkedin by  user " + File.CheckedOutByUser.LoginName + " using sharepoints default UI");
                    }
                    else
                    {
                        Logg(File.Title + " Is not Locked or CheckOut by any user");
                    }

                }
                else
                {
                    Logg("Lock By LoginName :-" + File.LockedByUser.LoginName);
                    Logg("Lock LockedDate  :-" + File.LockedDate);
                    Logg("Lock LockExpires :-" + File.LockExpires);
                    Logg("Lock LockId :-" + File.LockId);
                    Logg("Lock LockType :-" + File.LockType);
                }

            }
            catch (Exception ex)
            {
                Logg(ex);
            }
            //https://naptest.gsk.com/sites/fct/main/FCR%20Forms/Grande_Joseph_CEAD_2012_06_13.xml
        }


        protected void btnUnLockInfo_Click(object sender, EventArgs e)
        {
            try
            {
                SPWeb web = SPContext.Current.Web;
                SPFile File = web.GetFile(txtDocUrl.Text);
                if (String.IsNullOrEmpty(File.LockId))
                {
                    if (File.CheckOutType != SPFile.SPCheckOutType.None)
                    {
                        Logg(File.Title + " is Checked Out to user " + File.CheckedOutByUser.LoginName);
                        Logg(" Checked Out Type: " + File.CheckOutType);
                        Logg(" Checked Out CheckedOutDate: " + File.CheckedOutDate);
                        Logg(" File can be Checkedin by  user " + File.CheckedOutByUser.LoginName + " using sharepoints default UI");
                    }
                    else
                    {
                        Logg(File.Title + " Is not Locked by any user");
                    }

                }
                else if (!String.IsNullOrEmpty(File.LockId) && File.LockedByUser.ID != web.CurrentUser.ID)
                {
                    Logg(File.Title + " Is Locked By" + File.LockedByUser.LoginName + " and can only be UnLocked by same user.");
                    //Logg();
                }
                else
                {

                    Logg(File.Title + " Is Locked By" + File.LockedByUser.LoginName);
                    Logg("Start removing Lock");
                    File.ReleaseLock(File.LockId);
                    Logg("Lock Removed");
                }
            }
            catch (Exception Ex)
            {
                Logg(Ex);
            }
        }

        private void Logg(Exception Ex)
        {
            txtAreaLog.Text = txtAreaLog.Text + "Exception :- " + Ex.Message;
            txtAreaLog.Text = txtAreaLog.Text + "Stack Trace:- \n";
            txtAreaLog.Text = txtAreaLog.Text + Ex.StackTrace;
        }

        private void Logg(string p)
        {
            //throw new NotImplementedException();

            txtAreaLog.Text = txtAreaLog.Text + p + "\n";
        }

    }
}
